package com.zh.test.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Iterator;

@Component
@Slf4j
public class springUtils implements BeanFactoryPostProcessor {
    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        Iterator<String> it = configurableListableBeanFactory.getBeanNamesIterator();
        log.info("ioc容器中bean--------------------------------------------");
        while (it.hasNext()) {
            log.info(it.next());
        }
        beanFactory = configurableListableBeanFactory;
        log.info("ioc容器中bean--------------------------------------------");
    }

    public static <T> T getBean(Class<T> clazz) {
        T t = (T) beanFactory.getBean(clazz);
        return t;
    }
}
